using System;
using System.Runtime.InteropServices;
namespace SilverSea.Video.Tools
{
	public class Win32
	{
		[StructLayout(LayoutKind.Sequential, Pack = 1)]
		public struct AVICOMPRESSOPTIONS
		{
			[MarshalAs(UnmanagedType.I4)]
			public int type;
			[MarshalAs(UnmanagedType.I4)]
			public int handler;
			[MarshalAs(UnmanagedType.I4)]
			public int keyFrameEvery;
			[MarshalAs(UnmanagedType.I4)]
			public int quality;
			[MarshalAs(UnmanagedType.I4)]
			public int bytesPerSecond;
			[MarshalAs(UnmanagedType.I4)]
			public int flags;
			[MarshalAs(UnmanagedType.I4)]
			public int format;
			[MarshalAs(UnmanagedType.I4)]
			public int formatSize;
			[MarshalAs(UnmanagedType.I4)]
			public int parameters;
			[MarshalAs(UnmanagedType.I4)]
			public int parametersSize;
			[MarshalAs(UnmanagedType.I4)]
			public int interleaveEvery;
		}
		[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
		public struct AVISTREAMINFO
		{
			[MarshalAs(UnmanagedType.I4)]
			public int type;
			[MarshalAs(UnmanagedType.I4)]
			public int handler;
			[MarshalAs(UnmanagedType.I4)]
			public int flags;
			[MarshalAs(UnmanagedType.I4)]
			public int int_0;
			[MarshalAs(UnmanagedType.I2)]
			public short priority;
			[MarshalAs(UnmanagedType.I2)]
			public short language;
			[MarshalAs(UnmanagedType.I4)]
			public int scale;
			[MarshalAs(UnmanagedType.I4)]
			public int rate;
			[MarshalAs(UnmanagedType.I4)]
			public int start;
			[MarshalAs(UnmanagedType.I4)]
			public int length;
			[MarshalAs(UnmanagedType.I4)]
			public int initialFrames;
			[MarshalAs(UnmanagedType.I4)]
			public int suggestedBufferSize;
			[MarshalAs(UnmanagedType.I4)]
			public int quality;
			[MarshalAs(UnmanagedType.I4)]
			public int sampleSize;
			[MarshalAs(UnmanagedType.Struct)]
			public Win32.RECT rectFrame;
			[MarshalAs(UnmanagedType.I4)]
			public int editCount;
			[MarshalAs(UnmanagedType.I4)]
			public int formatChangeCount;
			[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
			public string name;
		}
		[StructLayout(LayoutKind.Sequential, Pack = 1)]
		public struct BITMAPINFOHEADER
		{
			[MarshalAs(UnmanagedType.I4)]
			public int size;
			[MarshalAs(UnmanagedType.I4)]
			public int width;
			[MarshalAs(UnmanagedType.I4)]
			public int height;
			[MarshalAs(UnmanagedType.I2)]
			public short planes;
			[MarshalAs(UnmanagedType.I2)]
			public short bitCount;
			[MarshalAs(UnmanagedType.I4)]
			public int compression;
			[MarshalAs(UnmanagedType.I4)]
			public int sizeImage;
			[MarshalAs(UnmanagedType.I4)]
			public int xPelsPerMeter;
			[MarshalAs(UnmanagedType.I4)]
			public int yPelsPerMeter;
			[MarshalAs(UnmanagedType.I4)]
			public int colorsUsed;
			[MarshalAs(UnmanagedType.I4)]
			public int colorsImportant;
		}
		public enum OpenFileMode
		{
			Read,
			Write,
			ReadWrite,
			ShareCompat = 0,
			ShareExclusive = 16,
			ShareDenyWrite = 32,
			ShareDenyRead = 48,
			ShareDenyNone = 64,
			Parse = 256,
			Delete = 512,
			Verify = 1024,
			Cancel = 2048,
			Create = 4096,
			Prompt = 8192,
			Exist = 16384,
			Reopen = 32768
		}
		[StructLayout(LayoutKind.Sequential, Pack = 1)]
		public struct RECT
		{
			[MarshalAs(UnmanagedType.I4)]
			public int left;
			[MarshalAs(UnmanagedType.I4)]
			public int top;
			[MarshalAs(UnmanagedType.I4)]
			public int right;
			[MarshalAs(UnmanagedType.I4)]
			public int bottom;
		}
		public enum SystemMetrics
		{
			CXSCREEN,
			CYSCREEN,
			CYCAPTION = 4,
			CYMENU = 15,
			CXFRAME = 32,
			CYFRAME
		}
		private static IntPtr intptr_0 = IntPtr.Zero;
		[DllImport("user32.dll")]
		public static extern int GetSystemMetrics([MarshalAs(UnmanagedType.I4)] Win32.SystemMetrics metric);
		[DllImport("user32.dll")]
		public static extern void SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter, int X, int Y, int width, int height, uint flags);
		public static void SetWinFullScreen(IntPtr hwnd)
		{
			Win32.SetWindowPos(hwnd, Win32.intptr_0, 0, 0, Win32.GetSystemMetrics(Win32.SystemMetrics.CXSCREEN), Win32.GetSystemMetrics(Win32.SystemMetrics.CYSCREEN), 64u);
		}
		[DllImport("ntdll.dll", CallingConvention = CallingConvention.Cdecl)]
		public static extern int memcpy(int dst, int src, int count);
		[DllImport("avifil32.dll")]
		public static extern void AVIFileInit();
		[DllImport("avifil32.dll")]
		public static extern void AVIFileExit();
		[DllImport("avifil32.dll", CharSet = CharSet.Unicode)]
		public static extern int AVIFileOpen(out IntPtr aviHandler, string fileName, Win32.OpenFileMode mode, IntPtr handler);
		[DllImport("avifil32.dll")]
		public static extern int AVIFileRelease(IntPtr aviHandler);
		[DllImport("avifil32.dll")]
		public static extern int AVIFileGetStream(IntPtr aviHandler, out IntPtr streamHandler, int streamType, int streamNumner);
		[DllImport("avifil32.dll")]
		public static extern int AVIFileCreateStream(IntPtr aviHandler, out IntPtr streamHandler, ref Win32.AVISTREAMINFO streamInfo);
		[DllImport("avifil32.dll")]
		public static extern int AVIStreamRelease(IntPtr streamHandler);
		[DllImport("avifil32.dll")]
		public static extern int AVIStreamSetFormat(IntPtr streamHandler, int position, ref Win32.BITMAPINFOHEADER format, int formatSize);
		[DllImport("avifil32.dll")]
		public static extern int AVIStreamStart(IntPtr streamHandler);
		[DllImport("avifil32.dll")]
		public static extern int AVIStreamLength(IntPtr streamHandler);
		[DllImport("avifil32.dll", CharSet = CharSet.Unicode)]
		public static extern int AVIStreamInfo(IntPtr streamHandler, ref Win32.AVISTREAMINFO streamInfo, int infoSize);
		[DllImport("avifil32.dll")]
		public static extern IntPtr AVIStreamGetFrameOpen(IntPtr streamHandler, ref Win32.BITMAPINFOHEADER wantedFormat);
		[DllImport("avifil32.dll")]
		public static extern IntPtr AVIStreamGetFrameOpen(IntPtr streamHandler, int wantedFormat);
		[DllImport("avifil32.dll")]
		public static extern int AVIStreamGetFrameClose(IntPtr getFrameObject);
		[DllImport("avifil32.dll")]
		public static extern IntPtr AVIStreamGetFrame(IntPtr getFrameObject, int position);
		[DllImport("avifil32.dll")]
		public static extern int AVIStreamWrite(IntPtr streamHandler, int start, int samples, IntPtr buffer, int bufferSize, int flags, IntPtr samplesWritten, IntPtr bytesWritten);
		[DllImport("avifil32.dll")]
		public static extern int AVISaveOptions(IntPtr window, int flags, int streams, [MarshalAs(UnmanagedType.LPArray, SizeConst = 0)] [In] IntPtr[] streamInterfaces, [MarshalAs(UnmanagedType.LPArray, SizeConst = 0)] [In] IntPtr[] options);
		[DllImport("avifil32.dll")]
		public static extern int AVISaveOptionsFree(int streams, [MarshalAs(UnmanagedType.LPArray, SizeConst = 0)] [In] IntPtr[] options);
		[DllImport("avifil32.dll")]
		public static extern int AVIMakeCompressedStream(out IntPtr compressedStream, IntPtr sourceStream, ref Win32.AVICOMPRESSOPTIONS options, IntPtr clsidHandler);
		public static int mmioFOURCC(string str)
		{
			return (int)((byte)str[0]) | (int)((byte)str[1]) << 8 | (int)((byte)str[2]) << 16 | (int)((byte)str[3]) << 24;
		}
		public static string decode_mmioFOURCC(int code)
		{
			char[] array = new char[4];
			for (int i = 0; i < 4; i++)
			{
				array[i] = (char)((byte)(code >> (i << 3) & 255));
				if (!char.IsLetterOrDigit(array[i]))
				{
					array[i] = ' ';
				}
			}
			return new string(array);
		}
		public static int AVISaveOptions(IntPtr stream, ref Win32.AVICOMPRESSOPTIONS options)
		{
			IntPtr[] array = new IntPtr[1];
			IntPtr[] array2 = new IntPtr[1];
			IntPtr intPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.AVICOMPRESSOPTIONS)));
			Marshal.StructureToPtr(options, intPtr, false);
			array[0] = stream;
			array2[0] = intPtr;
			int result = Win32.AVISaveOptions(IntPtr.Zero, 0, 1, array, array2);
			options = (Win32.AVICOMPRESSOPTIONS)Marshal.PtrToStructure(intPtr, typeof(Win32.AVICOMPRESSOPTIONS));
			Win32.AVISaveOptionsFree(1, array2);
			options.format = 0;
			options.parameters = 0;
			Marshal.FreeHGlobal(intPtr);
			return result;
		}
	}
}
